package com.ideaaedi.commonds.time;

import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

/**
 * 时间转换器
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Getter
public enum DateTimeConverter {

	/** item */
	COMPACT_DATE_TIME_MILLISECOND("yyyyMMddHHmmssSSS"),
	COMPACT_DATE_TIME("yyyyMMddHHmmss"),
	COMPACT_DATE("yyyyMMdd"),
	COMPACT_YEAR_MONTH("yyyyMM"),
	COMPACT_TIME("HHmmss"),
	COMPACT_HOUR_MINUTE("HHmm"),
	REGULAR_DATE_TIME_MILLISECOND("yyyy-MM-dd HH:mm:ss.SSS"),
	REGULAR_DATE_TIME("yyyy-MM-dd HH:mm:ss"),
	REGULAR_DATE("yyyy-MM-dd"),
	REGULAR_YEAR_MONTH("yyyy-MM"),
	REGULAR_TIME("HH:mm:ss"),
	REGULAR_HOUR_MINUTE("HH:mm"),
	SLASH_DATE_TIME_MILLISECOND("yyyy/MM/dd HH:mm:ss.SSS"),
	SLASH_DATE_TIME("yyyy/MM/dd HH:mm:ss"),
	SLASH_DATE("yyyy/MM/dd"),
	SLASH_YEAR_MONTH("yyyy/MM"),
	/** 效果形如：2023-12-19T17:01:24.102Z */
	UTC_DATE_TIME_MILLISECOND("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
	/** 效果形如：2023-12-19T17:01:24Z */
	UTC_DATE_TIME("yyyy-MM-dd'T'HH:mm:ss'Z'"),
	/** 效果形如：2023-12-19T17:01:24 */
	UTC_DATE_TIME_T("yyyy-MM-dd'T'HH:mm:ss"),
	UNDERLINE_DATE_TIME("yyyyMMdd_HHmmss"),
	;

	private final DateTimeFormatter formatter;

	DateTimeConverter(final String formatter) {
		this.formatter = new DateTimeFormatterBuilder().appendPattern(formatter)
				// 时代ChronoField.ERA取值范围[0, 1], 公元前=0，公元后=1
				.parseDefaulting(ChronoField.ERA, 1)
				.toFormatter();
	}
	
	public static void main(String[] args) {
		System.err.println(UTC_DATE_TIME_T.now());
	}

	/**
	 * 获取当前时间
	 */
	public String now() {
		return LocalDateTime.now().format(formatter);
	}
	
	/**
	 * 字符串 -> LocalDateTime
	 */
	public LocalDateTime convert2DateTime(String dateTime) {
		return LocalDateTime.parse(dateTime, formatter);
	}
	
	/**
	 * 字符串 -> LocalDate
	 */
	public LocalDate convert2Date(String date) {
		return LocalDate.parse(date, formatter);
	}
	
	/**
	 * 字符串 -> LocalTime
	 */
	public LocalTime convert2Time(String date) {
		return LocalTime.parse(date, formatter);
	}
	
	/**
	 * LocalDateTime -> 字符串
	 */
	public String convert2Str(LocalDateTime localDateTime) {
		return localDateTime.format(formatter);
	}
	
	/**
	 * LocalDate -> 字符串
	 */
	public String convert2Str(LocalDate localDate) {
		return localDate.format(formatter);
	}
	
	/**
	 * LocalTime -> 字符串
	 */
	public String convert2Str(LocalTime localTime) {
		return localTime.format(formatter);
	}
	
	/* ------------------------------------ 以下为过期方法 ------------------------------------  */
	
	/**
	 * 字符串 -> LocalDateTime
	 * <br />
	 * replaced by {@link DateTimeConverter#convert2DateTime(String)}
	 */
	@Deprecated
	public LocalDateTime convert2Obj(String dateTime) {
		return LocalDateTime.parse(dateTime, formatter);
	}
}
